import unittest
import os

from BaseRunner import BaseAutotestsRunner
import Autotests.Suites.AirBackupSuites as suites
from Autotests.WebDriver import AVAILABLE_DRIVERS, DRIVER
from Autotests.LocalMachine.Pages.AirClient.MainPage import WebMainPage
from Config.RunConfig import RunConfig, Options


class AirBackupRunner(BaseAutotestsRunner):
    def __init__(self):
        super(AirBackupRunner, self).__init__()

    def testsSuite(self):
        return unittest.TestLoader().loadTestsFromModule(suites)

    def setUpOptions(self):
        super(AirBackupRunner, self).setUpOptions()
        drivers = AVAILABLE_DRIVERS.keys()
        defauldDriver = RunConfig().get(Options.AirBackup.browser)
        helpMessage = '''selenium web driver [default]:%s available:
                             %s''' % (defauldDriver, drivers)

        self.addOption('-d', '--driver', type='choice', choices=drivers,
                       dest='driver', default=defauldDriver, help=helpMessage)

    def setUpStart(self):
        super(AirBackupRunner, self).setUpStart()
        RunConfig().set(Options.AirBackup.browser, self._runOptions.driver)
        os.environ['SELENIUM_SERVER_JAR'] = ".\selenium-server-standalone-2.39.0.jar"

    def reportName(self):
        return "%s AirBackup test report.html" % self._runOptions.driver

    def reportTitle(self):
        title = "Aibackup test report for %(build)s on %(browser)s"""

        try:
            browser = DRIVER().browser()
            build = WebMainPage().buildVersion()
        finally:
            DRIVER().quit()

        return title % dict(build=build, browser=browser)

if __name__ == '__main__':
    runner = AirBackupRunner()
    runner.startTests()
